# -*- coding: utf-8 -*-
from db_conn import *
import io
import time
from datetime import datetime, timedelta
import traceback
import re
from anatel_utils import *
from math import fabs, radians, cos, sin, asin, sqrt

date_now = datetime.now()
in_file_name_1 = "" + date_now.strftime("%Y-%m-%d") + "_file1.txt"
in_file_name_2 = "" + date_now.strftime("%Y-%m-%d") + "_file2.txt"
export_file_id = "" + date_now.strftime("%Y-%m-%d") + "-export-db-id.txt"
export_file = "" + date_now.strftime("%Y-%m-%d") + "-export-db.txt"

style_css="""
<style type="text/css">
#table-3{
    border: 1px solid #DFDFDF;background-color: #F9F9F9;width: 100%;-moz-border-radius: 3px;-webkit-border-radius: 3px;border-radius: 3px;font-family: Arial,"Bitstream Vera Sans",Helvetica,Verdana,sans-serif;color: #333;}
#table-3 td, #table-3 th {
    border-top-color: white;border-bottom: 1px solid #DFDFDF;color: #555;}
#table-3 th {
    text-shadow: rgba(255, 255, 255, 0.796875) 0px 1px 0px;font-family: Georgia,"Times New Roman","Bitstream Charter",Times,serif;font-weight: normal;padding: 7px 7px 8px;text-align: left;line-height: 1.3em;font-size: 14px;}
#table-3 td {
    font-size: 12px;padding: 4px 7px 2px;vertical-align: top;
}</style>
"""

def float_eq(a, b, epsilon=0.00000001):
    return abs(a - b) < epsilon

def transCord(coord):
    #14S122800
    #=-(14+12/60+28/3600)
    
    dd = int(coord[0:2])
    mm = float(coord[3:5]) / 60
    ss = float(float(coord[5:7] + "." + coord[7:]) / 3600)
    
    #print "dd=%s" % (dd)
    #print "mm=%s" % (mm)
    #print "ss=%s" % (ss)
    
    '''
    dd = int(coord[0:2])
    mm = round(int(coord[3:5])/60,6)
    ss = round(float(coord[5:7] + "." + coord[7:])/3600,6)
    '''
    
    coord_new = float(dd + mm + ss)
    if(coord.find("S")>-1 or coord.find("W")>-1):
        coord_new=coord_new*-1
    
    return coord_new
        
def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    km = 6367 * c
    return km
        

  
if __name__ == '__main__':
    if not len(sys.argv) < 2:
        
        for v in sys.argv[1:]:
            try:
                print transCord(v)
            except:
                exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
                traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                                  limit=2, file=sys.stdout)      
    start_total = time.time()
    
    print "Comecando" % ()


    print "(Total Tempo Gasto para Enlaces: %s)" % ((time.time() - start_total))